//
//  PersonViewController.m
//  DWL_Vogue
//
//  Created by dllo on 15/10/22.
//  Copyright (c) 2015年 Forlosan. All rights reserved.
//

#import "PersonViewController.h"
#import "VogueHeader.h"
#import "WidthAndHeight.h"
#import "SetPersonTableViewCell.h"
#import "CollectViewController.h"
#import "SetViewController.h"
#import "Singleton.h"

@interface PersonViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain)UITableView *personTableView;
@property(nonatomic, assign)NSUInteger num;
@property(nonatomic, retain)NSArray *imageArr;
@property(nonatomic, retain)NSArray *nameArr;
@property(nonatomic, assign)NSInteger row;
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UIImageView *avatarImageView;

@end

@implementation PersonViewController
- (void)dealloc {
    [_personTableView release];
    [_nameArr release];
    [_imageArr release];
    [_nameLabel release];
    [_avatarImageView release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.imageArr = @[@"chuan.jpg", @"chi.jpg", @"wan.jpg", @"big.jpg", @"set.png"];
        self.nameArr = @[@"潮流 · 穿", @"美食 · 吃", @"旅游 · 玩", @"生活 · BIG", @"设置"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.title = @"个人";
//    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor yellowColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Marion-Italic" size:24.0],UITextAttributeFont,nil]];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:39 / 255.0 green:38 / 255.0 blue:55 / 255.0 alpha:1];
    UILabel *personTitle = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 30, 15, 60, 20)];
    personTitle.text = @"个人";
    personTitle.textAlignment = NSTextAlignmentCenter;
    personTitle.textColor = [UIColor whiteColor];
    personTitle.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:15];
    [self.navigationController.navigationBar addSubview:personTitle];
    [personTitle release];
  
//    HeadView
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    [self.view addSubview:headView];
    [headView release];
    
//    backImageView
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    backImageView.image = [UIImage imageNamed:@"person.jpg"];
    [headView addSubview:backImageView];
    [backImageView release];
    
//    Avatar
//    self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, HEIGHT / 6, 50, 50)];
//    self.avatarImageView.backgroundColor = [UIColor lightGrayColor];
//    self.avatarImageView.layer.borderWidth = 1;
//    self.avatarImageView.layer.borderColor = [UIColor whiteColor].CGColor;
//    self.avatarImageView.layer.cornerRadius = 25;
//    [headView addSubview:self.avatarImageView];
//    [_avatarImageView release];
//    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[Singleton shareSingleton].urlStr]];
//    self.avatarImageView.layer.masksToBounds = YES;
    
    //  nameLabel
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, HEIGHT / 6 + 10, 150, 30)];
    [headView addSubview:self.nameLabel];
    self.nameLabel.text = [Singleton shareSingleton].titleStr;
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.font = [UIFont systemFontOfSize:12];
    [_nameLabel release];
    
//    UIButton *avatarButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    avatarButton.frame = CGRectMake(20, HEIGHT / 6, 50, 50);
//    [headView addSubview:avatarButton];
//    [avatarButton setImage:[UIImage imageNamed:@"avatar.png"] forState:UIControlStateNormal];
//    avatarButton.layer.borderWidth = 1;
//    avatarButton.layer.borderColor = [UIColor whiteColor].CGColor;
//    avatarButton.layer.cornerRadius = 25;
//    avatarButton.layer.masksToBounds = YES;
//    [avatarButton addTarget:self action:@selector(avatarButtonAction) forControlEvents:UIControlEventTouchUpInside];
//    TableView
    self.personTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.personTableView];
    self.personTableView.tableHeaderView = headView;
    [_personTableView release];
    self.personTableView.backgroundColor = [UIColor colorWithRed:239 / 255.0 green:238 / 255.0 blue:244 / 255.0 alpha:1];
    self.personTableView.delegate = self;
    self.personTableView.dataSource = self;
    

    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [sandBoxPath stringByAppendingPathComponent:@"MY.sqlite"];
    
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"开启成功");
    } else {
        NSLog(@"开启失败");
    }
    NSLog(@"%@", dbPath);
    
//    CreateData
    self.num = 100;
    [self createData];
    
}

- (void)avatarButtonAction {
    
}

- (void)createData {
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    } else if (section == 1){
        return 1;
    } else {
        return 0;
    }
}

//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *reuse = @"reuse";
//    SetPersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
//    if (!cell) {
//        cell = [[SetPersonTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
//    }
//    cell.setImageView.image = [UIImage imageNamed:self.imageArr[indexPath.row]];
//    cell.setImageView.backgroundColor = [UIColor whiteColor];
//    cell.setLabel.text = self.nameArr[indexPath.row];
//    cell.rightImageView.image = [UIImage imageNamed:@"right.png"];
//    self.row = indexPath.row;
//    return cell;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    SetPersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[SetPersonTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if (indexPath.section == 0) {
        cell.setImageView.image = [UIImage imageNamed:self.imageArr[indexPath.row]];
        cell.setLabel.text = self.nameArr[indexPath.row];
        self.row = indexPath.row;
        return cell;
    } else if (indexPath.section == 1) {
        cell.setImageView.image = [UIImage imageNamed:self.imageArr[indexPath.row + 4]];
        cell.setImageView.backgroundColor = [UIColor whiteColor];
        cell.setLabel.text = self.nameArr[indexPath.row + 4];
        self.row = indexPath.row + 4;
        return cell;
    } else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.num = indexPath.row;
    if (indexPath.section == 1) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        SetViewController *set = [[SetViewController alloc] init];
        [self presentViewController:set animated:YES completion:nil];
        [set release];
        
    } else {
        if (indexPath.row == 2) {
            [self.personTableView reloadData];
            CollectViewController *collect = [[CollectViewController alloc] init];
            collect.imageStr = self.imageArr[indexPath.row];
            collect.str = self.nameArr[indexPath.row];
            NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/MyCollect/play.plist"];
            NSMutableArray *playArr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            collect.collectArr = playArr;
            [self presentViewController:collect animated:YES completion:nil];
            [collect release];
        }
        
    [self.personTableView reloadData];
    CollectViewController *collect = [[CollectViewController alloc] init];
    collect.imageStr = self.imageArr[indexPath.row];
    collect.str = self.nameArr[indexPath.row];
    [self presentViewController:collect animated:YES completion:nil];
    [collect release];
        
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
//    if (indexPath.row == 4) {
//        return 50;
//    } else if (indexPath.row == self.num) {
//        return 65;
//    } else {
//    return 50;
//    }
    
    return 40;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 10;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"         我的收藏";
    } else  {
        return @" ";
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.personTableView.contentSize = CGSizeMake(0, HEIGHT - 108);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }
    
    // Create label with section title
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(12, 5, 300, 20);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    label.text = sectionTitle;
    
    // Create header view and add label as a subview
    UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)] autorelease];
    [sectionView setBackgroundColor:[UIColor colorWithRed:239 / 255.0 green:238 / 255.0 blue:244 / 255.0 alpha:1]];
    [sectionView addSubview:label];
    return sectionView;
}

#pragma mark  ② 视图将要出现
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"%s", __FUNCTION__);
    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[Singleton shareSingleton].urlStr]];
    self.nameLabel.text = [Singleton shareSingleton].titleStr;
    NSLog(@"%@", [Singleton shareSingleton].titleStr);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
